package pl.edu.agh.anomalyids.lerad.aspect.implementation;

import pl.edu.agh.anomalyids.lerad.aspect.enums.AspectType;
import pl.edu.agh.anomalyids.lerad.aspect.enums.Match;
import pl.edu.agh.anomalyids.network.Packet;
import pl.edu.agh.anomalyids.network.Packet.Protocol;
import pl.edu.agh.anomalyids.network.header.Header;
import pl.edu.agh.anomalyids.network.header.impl.TCPHeader;
import pl.edu.agh.anomalyids.network.header.impl.UDPHeader;

public class SrcPortAspect extends PortAspect {

	private static final long serialVersionUID = -125678170657750913L;

	public SrcPortAspect(Integer port) {
		super(port, AspectType.SRCPORT);
	}

	@Override
	public Match packetMatchesAspect(Packet packet) {
		Header header = packet.getProtocols().get(Protocol.TCP);
		if (header != null) {
			TCPHeader tcp = (TCPHeader) header;
			if (validate(tcp.getSrcPort())) {
				return Match.SAME;
			} else {
				return Match.DIFFER;
			}
		} else {
			header = packet.getProtocols().get(Protocol.UDP);
			if (header != null) {
				UDPHeader udp = (UDPHeader) header;
				if (validate(udp.getSrcPort())) {
					return Match.SAME;
				} else {
					return Match.DIFFER;
				}
			} else {
				return Match.UNKNOWN;
			}
		}
	}

}
